import base64
import sys
import time

from django.shortcuts import render

from django.http import HttpResponse, response, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.views import APIView

from apply import oss
import os
import moviepy.editor as mp
import wave
import os
import numpy as np

# Create your views here.


@csrf_exempt
def speech(request):
    if request.method == 'GET':
        if (str(request.GET.get('lang')) == 'Ch'):
            print("fuck")
            str1 = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis'\n"
            str2 = "       xmlns:mstts='https://www.w3.org/2001/mstts' xml:lang='zh-CN'>\n"
            str3 = "    <voice name='zh-CN-XiaoxiaoNeural'>\n"
            str4 = "        " + str(request.GET.get('speech')) + "\n"
            str5 = "    </voice>\n"
            str6 = "</speak>\n"
            fo = open("apply/speech/ssml.xml", "w", encoding='UTF-8')
            fo.write(str1)
            fo.write(str2)
            fo.write(str3)
            fo.write(str4)
            fo.write(str5)
            fo.write(str6)
            fo.close()
            with open('apply/speech/new.py', 'r', encoding='utf-8') as f:
                exec(f.read())
            # 获取前端上传的图片，并读取二进制数据
            file = open(r'apply/speech/file.mp3', 'rb').read()
        else:
            str1 = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis'\n"
            str2 = "       xmlns:mstts='https://www.w3.org/2001/mstts' xml:lang='en-US'>\n"
            str3 = "    <voice name='en-US-AmberNeural'>\n"
            str4 = "        " + str(request.GET.get('speech')) + "\n"
            str5 = "    </voice>\n"
            str6 = "</speak>\n"
            fo = open("apply/speech/ssml.xml", "w", encoding='UTF-8')
            fo.write(str1)
            fo.write(str2)
            fo.write(str3)
            fo.write(str4)
            fo.write(str5)
            fo.write(str6)
            fo.close()
            with open('apply/speech/new.py', 'r', encoding='utf-8') as f:
                exec(f.read())
            # 获取前端上传的图片，并读取二进制数据
            file = open(r'apply/speech/file.mp3', 'rb').read()
        # 转存到oss
        res = oss.update_fil_file(file)
        if res:
            return JsonResponse({'errno': 0, 'msg': "成功", 'src': res})
        else:
            return JsonResponse({'errno': -1, 'msg': "失败"})





title = "123"
content = "456"
views_uuid = "789"
@csrf_exempt
def virtual(request):
    if request.method == 'GET':
        global title
        title = request.GET.get('title')
        global content
        content = request.GET.get('content')
        # os.system("python apply/virtual/run/alibabacloud_sample/sample.py")
        from apply.virtual.run.alibabacloud_sample.sample import Sample
        Sample.main(sys.argv[0:])
        from apply.virtual.run.alibabacloud_sample.sample import uuid
        global views_uuid
        views_uuid = uuid
        print(views_uuid)
        # time.sleep(100)
        from apply.virtual.search.alibabacloud_sample.sample import Sample
        Sample.main(sys.argv[0:])
        from apply.virtual.search.alibabacloud_sample.sample import urls
        print(urls)
        return JsonResponse({'errno': 0, 'msg': urls})


from django.http import HttpResponse,JsonResponse
import os

global pass_text

# Create your views here.


def test(request):
    return HttpResponse("登录页面")


def passPic(text):
    if len(text) > 127:
        text = text[0:126]
    print(text)
    path = r"C:/Users/86138/Desktop/SD/outputs/txt2img-samples/" + text + "/1.png"
    # print(path)
    with open(path, 'rb') as f:
        data = f.read()
        encode_str = base64.b64encode(data)
    encode_str = str(encode_str, 'utf-8')
    return encode_str


def uploadPic(text):
    if len(text) > 127:
        text = text[0:126]
    print(text)
    path = r"C:/Users/86138/Desktop/SD/outputs/txt2img-samples/" + text + "/1.png"
    # path = r"C:/Users/86138/Desktop/SD/outputs/txt2img-samples/A painting of Mickey looking lost in the forest, surrounded by trees and bushes/1.png"
    file = open(path, 'rb').read()
    res = oss.update_fil_image(file)
    return res


def draw(request):
    if request.method == 'GET':
        text = request.GET.get('text')
        print(text)
        text1 = "\"" + str(text) + "\""
        print(text1)
        os.system("cd C:/Users/86138/Desktop/SD && conda activate ldm && python optimizedSD/optimized_txt2img.py --prompt " + str(text1))
        if len(text) > 127:
            text = text[0:126]
        pic_str = uploadPic(text)
        print(pic_str)
    return JsonResponse({'status_code': 0, 'codeStr': pic_str})




